package com.zbbt.sportslottery.ui.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.UmengShare;

/**
 * 
 * @ClassName: RecomActivity
 * @Description: 推荐
 * @author：GaomingShuo
 * @date：2015-4-29 下午3:59:13
 * @version V1.0
 */
public class RecomActivity extends SimpleBaseActivity {
	private TextView reshare;
	private UmengShare umengShare;
	private String summaryTx;
	ImageView imageView;
	Toolbar toolbar;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_recommend);
	}

	@Override
	protected void initView() {
		reshare = (TextView) findViewById(R.id.action_share);
		imageView = (ImageView) findViewById(R.id.imageview);
		umengShare = new UmengShare(this);
		summaryTx = "http://web.zgtcb.com/img/fjcode.jpg";
		toolbar = (Toolbar) findViewById(R.id.toolbar);
		// Bitmap takeScreenShot = takeScreenShot(RecomActivity.this);
		// imageView.setImageBitmap(takeScreenShot);
	}

	@Override
	protected void setListener() {
		reshare.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.action_share:
			// fa1
			Bitmap drawableToBitamp = takeScreenShot(RecomActivity.this);
			// fa2
			// Drawable drawable =
			// context.getResources().getDrawable(R.drawable.tow_code);
			// Bitmap drawableToBitamp = drawableToBitamp(drawable);
//			umengShare.openshareimg(summaryTx, drawableToBitamp);
			String url="http://a.app.qq.com/o/simple.jsp?pkgname=com.zbbt.sportslottery";
			String summaryTx="资讯全方位，赛事零距离。即时11选5开奖信息及图表，快速寻找身边的投注网点，趣味生动的彩票段子，最新的体彩官方活动尽在中国体彩手机报";
			String shareTitle=" 福建体彩手机报";
			umengShare.openshare(url,summaryTx,shareTitle,null,0);
			break;

		default:
			break;
		}
	}

	private Bitmap drawableToBitamp(Drawable drawable) {
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		System.out.println("Drawable转Bitmap");
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		// 注意，下面三行代码要用到，否在在View或者surfaceview里的canvas.drawBitmap会看不到图
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		drawable.draw(canvas);
		return bitmap;
	}

	private Bitmap takeScreenShot(Activity activity) {
		// View是你需要截图的View
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap b1 = view.getDrawingCache();

		// 获取状态栏高度
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = toolbar.getHeight() * 2;

		// 获取屏幕长和高
		int width = activity.getWindowManager().getDefaultDisplay().getWidth();
		int height = activity.getWindowManager().getDefaultDisplay().getHeight();
		// 去掉标题栏
		Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
		view.destroyDrawingCache();
		return b;
	}

	private Bitmap getViewBitmap(View v) {
		v.clearFocus();
		v.setPressed(false);

		boolean willNotCache = v.willNotCacheDrawing();
		v.setWillNotCacheDrawing(false);

		// Reset the drawing cache background color to fully transparent
		// for the duration of this operation
		int color = v.getDrawingCacheBackgroundColor();
		v.setDrawingCacheBackgroundColor(0);

		if (color != 0) {
			v.destroyDrawingCache();
		}
		v.buildDrawingCache();
		Bitmap cacheBitmap = v.getDrawingCache();
		if (cacheBitmap == null) {
			return null;
		}

		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

		// Restore the view
		v.destroyDrawingCache();
		v.setWillNotCacheDrawing(willNotCache);
		v.setDrawingCacheBackgroundColor(color);

		return bitmap;
	}

	@Override
	protected void initData() {

	}

}
